<?php
    $xtpl_edit  = new XTemplate("view/edit.htm");
    
    $title  = "Thay đổi thông tin cá nhân";
    
    $query  = "SELECT * FROM account WHERE username='admin'";
    $re     = mysql_query($query);
    $row    = mysql_fetch_array($re);
    
    $date       = "SELECT DAY(birthday), MONTH(birthday), YEAR(birthday) FROM account WHERE username='admin'";
    $re_date    = mysql_query($date);
    $row_date   = mysql_fetch_row($re_date);   

    $day    =   '<select name="day">';
    $day    .=  '<option value="0">Ngày</option>';
    for($i=1; $i<=31; $i++){
        $day    .= '<option value="' . $i . '">' . $i . '</option>';
        if($i == $row_date[0]){
            $day    .= '<option selected="' . $row_date[0] . '">' . $row_date[0] . '</option>';
        }
    }
    $day    .= '</select>';
    
    $month  =   '<select name="month">';
    $month  .=  '<option value="0">Tháng</option>';
    for($i=1; $i<=12 ; $i++){
        $month  .= '<option value="' . $i . '">' . $i . '</option>';
        if($i == $row_date[1]){
            $month  .= '<option selected="' . $row_date[1] . '">' . $row_date[1] . '</option>';
        }
    }
    $month  .= '</select>';
    
    $year   =   '<select name="year">';
    $year   .=  '<option value="0">Năm</option>';
    for($i=1950; $i<=2011 ; $i++){
        $year   .= '<option value="' . $i . '">' . $i . '</option>';
        if($i == $row_date[2]){
            $year   .= '<option selected="' . $row_date[2] . '">' . $row_date[2] . '</option>';
        }
    }
    $year   .= '</select>'; 
    
    $xtpl_edit  -> assign('fullname', $row['fullname']);
    $xtpl_edit  -> assign('day', $day);
    $xtpl_edit  -> assign('month', $month);
    $xtpl_edit  -> assign('year', $year);
    $xtpl_edit  -> assign('phone', $row['phone']);
    $xtpl_edit  -> assign('email', $row['email']);
    
    if(isset($_POST['submit'])){
        if(($_POST['fullname']!=NULL)&&($_POST['day']!=NULL)&&($_POST['month']!=NULL)&&($_POST['year'])&&($_POST['phone'])&&($_POST['email'])){
            $fullname   = $_POST['fullname'];
            $day            = $_POST['day'];
            $day            = ($day == 0) ? ($day = '00') : $day;
            $month          = $_POST['month'];
            $month          = ($month == 0) ? ($month = '00') : $month;
            $year           = $_POST['year'];
            $year           = ($year == 0) ? ($year = '0000') : $year;
            $birthday       = $year . '-' . $month . '-' . $day;
            $phone      = $_POST['phone'];
            $email      = $_POST['email'];
            
            $sql    = "UPDATE account SET fullname='" . $fullname . "', birthday='" . $birthday . "', phone='" . $phone . "', email='" . $email . "' WHERE id='" . $row['id'] . "'";
            $result = mysql_query($sql);
            if($result){
                $error  = "Đã thay đổi thông tin";
            }
        }
        else{
            $error  = "Bạn chưa điền đầy đủ thông tin";
        }
    }
    
    $xtpl_edit  -> assign('error', $error);
    $xtpl_edit  -> parse("EDIT");
    $content    = $xtpl_edit -> text("EDIT");
?>